using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Markup;
using System.Windows.Xps.Packaging;
using Common;
using MainView.MainControls.Chart;
using Report;
using SExperiment;
using SWellPlate;

namespace MainView.Windows
{
	public partial class ReportWindowCustom : Window, IComponentConnector
	{
		private int Mode;

		private BaseCurveChart Chart;

		private Experiment Experiment;

		private List<ChartData> SelectedData;

		public ReportWindowCustom(Experiment experiment, int mode, BaseCurveChart chart, List<ChartData> selectedData)
		{
			Experiment = experiment;
			Mode = mode;
			Chart = chart;
			SelectedData = selectedData;
			InitializeComponent();
			bntPrint.IsEnabled = false;
			BackgroundWorker backgroundWorker = new BackgroundWorker();
			backgroundWorker.DoWork += worker_DoWork;
			backgroundWorker.RunWorkerCompleted += worker_RunWorkerCompleted;
			backgroundWorker.RunWorkerAsync();
			tbError.Text = "Load...";
			if (chart is AMPCurveChart)
			{
				Chart = new AMPCurveChart();
			}
			else if (chart is RawCurveChart)
			{
				Chart = new RawCurveChart();
			}
			else if (chart is RawMeltCurveChart)
			{
				Chart = new RawMeltCurveChart();
			}
			else if (chart is PeekCurveChart)
			{
				Chart = new PeekCurveChart();
			}
			else if (chart is XCurveChart)
			{
				Chart = new XCurveChart();
			}
			else if (chart is NormalizationPeakCurveChart)
			{
				Chart = new NormalizationPeakCurveChart();
			}
			else if (chart is NormalCurveChart)
			{
				Chart = new NormalCurveChart();
			}
			else if (chart is ShiftCurveChart)
			{
				Chart = new ShiftCurveChart();
			}
			else if (chart is DifferenceCurveChart)
			{
				Chart = new DifferenceCurveChart();
			}
			if (Chart != null)
			{
				Chart.PrintMode = true;
				Chart.Subset = chart.Subset;
				Chart.ColorMode = chart.ColorMode;
				Chart.ThresholdVisible = chart.ThresholdVisible;
				Chart.MinX = chart.MinX;
				Chart.MaxX = chart.MaxX;
				Chart.MinY = chart.MinY;
				Chart.MaxY = chart.MaxY;
				Chart.TitleX = chart.TitleX;
				Chart.TitleY = chart.TitleY;
				Chart.TitleXFormat = chart.TitleXFormat;
				Chart.TitleYFormat = chart.TitleYFormat;
				Chart.UltraChart.Chart.Dock = DockStyle.None;
				Chart.UltraChart.Chart.Width = 500;
				Chart.UltraChart.Chart.Height = 350;
				Chart.DisplayAllSeries = true;
				Chart.Targets = chart.Targets;
			}
		}

		private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			string text = e.Result as string;
			if (!File.Exists(text))
			{
				tbError.Text = text;
				return;
			}
			tbError.Text = string.Empty;
			XpsDocument xpsDocument = new XpsDocument(text, FileAccess.Read);
			IDocumentPaginatorSource fixedDocumentSequence = xpsDocument.GetFixedDocumentSequence();
			xpsDocument.Close();
			documentViewer.Document = fixedDocumentSequence;
			bntPrint.IsEnabled = true;
		}

		private void worker_DoWork(object sender, DoWorkEventArgs e)
		{
			try
			{
				string text = Utility.GetSystemTempFile() + ".xps";
				string sourceFileName = null;
				string systemTempFile = Utility.GetSystemTempFile();
				string text2 = systemTempFile + ".docx";
				if (Mode == 1)
				{
					sourceFileName = Utility.GetReportTemplatePath() + "Report_Lizhu.docx";
				}
				else if (Mode == 2)
				{
					sourceFileName = Utility.GetReportTemplatePath() + "Report_Guoce.docx";
				}
				File.Copy(sourceFileName, text2, true);
				File.SetAttributes(text2, File.GetAttributes(text2) & ~FileAttributes.ReadOnly);
				int num = 0;
				if (Mode == 2)
				{
					PatientReportCreator_Guoce patientReportCreator_Guoce = new PatientReportCreator_Guoce();
					patientReportCreator_Guoce.WrodFile = text2;
					patientReportCreator_Guoce.Experiment = Experiment;
					patientReportCreator_Guoce.ChartDataList = SelectedData;
					num = patientReportCreator_Guoce.ExportSamplesResultToWord(CreateSampleChart);
				}
				if (num == 0)
				{
					if (WordOperator.ExportToXPS(text2, text))
					{
						e.Result = text;
					}
					else
					{
						e.Result = "Create XPS File Error.";
					}
				}
				else
				{
					e.Result = "Create Report Error.";
				}
			}
			catch (Exception ex)
			{
				e.Result = ex.StackTrace;
			}
		}

		private string CreateSampleChart(List<Sample> samples, double width, double height, List<int> TubeNoList, List<int> ChannelNoList)
		{
			string path = string.Empty;
			if (Chart != null)
			{
				base.Dispatcher.Invoke((Action)delegate
				{
					Chart.UltraChart.Chart.Width = (int)width;
					Chart.UltraChart.Chart.Height = (int)height;
					Chart.Targets = SelectedData;
					Chart.RefreshSeries();
					path = Utility.GetSystemTempFile();
					Chart.UltraChart.SaveAsImage(path);
				}, new object[0]);
			}
			return path;
		}

		private void bntPrintSelect_Click(object sender, RoutedEventArgs e)
		{
			documentViewer.Print();
		}
	}
}
